package com.avic.rong.search.fragment.searchResultFragments;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.goodsSort.bean.showRvBean.GoodsSortSearchAllBzBean;
import com.avic.rong.search.activity.SearchResaultActivity;
import com.avic.rong.search.adapter.searchResultFragmentsAdapters.SearchWendaShrinkRvAdapter;
import com.avic.rong.search.api.secondViewApi.SearchHomeQuanBuApi;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class SearchWenDaFragment extends Fragment implements OnRefreshListener, OnLoadMoreListener {

    @BindView(R.id.wenda_rec)
    RecyclerView wendaRec;
    Unbinder unbinder;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.search_vp_peixun_tv)
    TextView searchVpPeixunTv;
    @BindView(R.id.beijing_image)
    ImageView beijing_image;
    public boolean isRefresh = false;
    private Integer pagenum = 1;
    private List<GoodsSortSearchAllBzBean.DataBean> homePageQuanBuWenDaList = new ArrayList<>();
    private SearchHomeQuanBuApi searchAllApi;
    int pageNo=1;
    int pagesize=10;
    private String currentText;
    private SearchWendaShrinkRvAdapter wenDaAdapter;

    public SearchWenDaFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_search_wen_da, container, false);

        ButterKnife.bind(this, view);
        homePageQuanBuWenDaList.clear();
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);
        DividerItemDecoration divider = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.divider_follow));
        wendaRec.addItemDecoration(divider);
        wendaRec.setLayoutManager(new LinearLayoutManager(getActivity()));
        wenDaAdapter = new SearchWendaShrinkRvAdapter(getActivity(), homePageQuanBuWenDaList);
        wendaRec.setAdapter(wenDaAdapter);
        loadData();
        return view;
    }

    public void loadData() {
        currentText = SearchResaultActivity.nowSearchStr;
        initQuanBuData(currentText, pageNo);
    }

    public void initQuanBuData(String keyword, int pageNo) {
        searchAllApi = new SearchHomeQuanBuApi((BaseActivity) getActivity(), searchHomeQuanBuListener, "quanbu", 11, keyword, pageNo, pagenum);
        Log.e("cjx", "bzkeyWord:" + keyword);
        searchAllApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(searchAllApi);
    }

    //获取标准数据监听
    HttpOnNextListener<BaseResultEntity> searchHomeQuanBuListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals("200")) {

                Log.e("cjx", "biaozhun数据：" + subjects.getMsg());
                return;
            }
            ArrayList<GoodsSortSearchAllBzBean> searchHomeQuanBuList = (ArrayList<GoodsSortSearchAllBzBean>) subjects.getData();
            List<GoodsSortSearchAllBzBean.DataBean> data = searchHomeQuanBuList.get(4).getData();
            homePageQuanBuWenDaList.addAll(data);
            if (homePageQuanBuWenDaList.size()>0) {
                refreshLayout.setVisibility(View.VISIBLE);
                searchVpPeixunTv.setVisibility(View.GONE);
                beijing_image.setVisibility(View.GONE);
            }else{
                refreshLayout.setVisibility(View.GONE);
                searchVpPeixunTv.setVisibility(View.VISIBLE);
                beijing_image.setVisibility(View.VISIBLE);
            }

         wenDaAdapter.notifyDataSetChanged();
            if (isRefresh) {
                refreshLayout.finishRefresh();

            } else {
                refreshLayout.finishLoadMore();
            }
        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        homePageQuanBuWenDaList.clear();
        isRefresh = true;
        pagenum = 1;
        searchAllApi = new SearchHomeQuanBuApi((SearchResaultActivity) getActivity(), searchHomeQuanBuListener, "quanbu", 11,currentText , pageNo, pagesize);
        HttpManager.getInstance().doHttpDeal(searchAllApi);
        refreshLayout.finishRefresh(2000);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        isRefresh = false;
        pagenum = pagenum + 1;
        searchAllApi = new SearchHomeQuanBuApi((SearchResaultActivity) getActivity(), searchHomeQuanBuListener, "quanbu", 11, currentText, pageNo, pagenum);
        HttpManager.getInstance().doHttpDeal(searchAllApi);
        refreshLayout.finishLoadMore(3000);
    }
}
